package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/4/17 11:09
 * https://leetcode-cn.com/problems/max-area-of-island/
 */
public class Title695 {

    int n;
    int m;

    public int maxAreaOfIsland(int[][] grid) {
        n = grid.length;
        m = grid[0].length;
        int max = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 1) {
                    max = Math.max(max, dfs(grid, i, j));
                }
            }
        }
        return max;
    }

    private int dfs(int[][] grid, int i, int j) {
        if (i < 0 || i >= n || j < 0 || j >= m || grid[i][j] != 1 ) {
            return 0;
        }
        grid[i][j] = 0;
        int up = dfs(grid, i - 1, j);
        int down = dfs(grid, i + 1, j);
        int left = dfs(grid, i, j - 1);
        int right = dfs(grid, i, j + 1);
        return up + down + left + right + 1;
    }

    public static void main(String[] args) {

    }
}
